using Microsoft.VisualBasic.CompilerServices;
using Zhaoxi.Zhihu.Core.AppUserAggregate.Event;
using Zhaoxi.Zhihu.Core.Common;
using Zhaoxi.Zhihu.SharedKerel.Domain;
using Zhaoxi.Zhihu.SharedKerel.Result;

namespace Zhaoxi.Zhihu.Core.AppUserAggregate.Entites;

public class AppUser:BaseAuditEntity,IAggregateRoot
{
    private readonly List<FollowUser> followees = [];
    private readonly List<FollowUser> _followers = [];
    private readonly List<FollowQuestion> followQuestions = [];
    public AppUser()
    {
        
    }

    public AppUser(int userId)
    {
        Id = userId;
    }
    
    public string? Nickname { get; set; }
    
    public string? Avatar { get; set; }
    
    public string? Bio { get; set; }
    
    //关注列表
    public ICollection<FollowUser> Followees { get; set; } = new List<FollowUser>();
    //粉丝列表
    public ICollection<FollowUser> Followers { get; set; } = new List<FollowUser>();
    //关注问题
    
    private readonly List<FollowQuestion> _followQuestions=new();
    public IReadOnlyCollection<FollowQuestion> FollowQuestions => _followQuestions.AsReadOnly();



    /// <summary>
    /// 添加关注
    /// </summary>
    /// <param name="questionID"></param>
    /// <returns></returns>
    public IResult AddFollowQuestion(int questionID)
    {
        if (_followQuestions.Any(fq => fq.QuestionId == questionID)) return Result.Invalid("问题已关注");

        var followQuestion = new FollowQuestion
        {
            QuestionId = questionID,
            FollowDate = DateTimeOffset.Now
        };
        _followQuestions.Add(followQuestion);
        
        AddDomainEvent(new FollowQuestionAddedEvent(followQuestion));
        
        return Result.Success();
    }

    public IResult RemoveFollowQuestion(int questionID)
    {
        var followQuestion = _followQuestions.FirstOrDefault(fq => fq.QuestionId == questionID);
        if (followQuestion!=null) _followQuestions.Remove(followQuestion);
        
        RemoveDomainEvent(new FollowQuestionRemovedEvent(followQuestion));
        return Result.Success();
        
    }

    public IResult RemoveFollowee(int requestFolloweeId)
    {
        var followee = followees.FirstOrDefault(f => f.Id == requestFolloweeId);
        if (followee != null) followees.Remove(followee);
        return Result.Success();
    }
}